package com.github.tommas.admintpl.controller;

import com.github.tommas.admintpl.bean.model.User;
import com.github.tommas.admintpl.bean.vo.PaginationQuery;
import com.github.tommas.admintpl.bean.vo.SearchUserParam;
import com.github.tommas.admintpl.common.CommonResult;
import com.github.tommas.admintpl.common.CommonResultCode;
import com.github.tommas.admintpl.common.PaginationData;
import com.github.tommas.admintpl.security.permission.Permissions;
import com.github.tommas.admintpl.service.UserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController implements RequirePermissionController {
    @Autowired
    UserService userService;

    @RequiresPermissions(Permissions.USER_READ)
    @PostMapping("/list")
    public CommonResult<PaginationData<User>> getUserList(@RequestBody PaginationQuery<SearchUserParam> pq) {
        return CommonResult.success(userService.getUserList(pq));
    }

    @RequiresPermissions(Permissions.USER_READ)
    @GetMapping("/{id}")
    public CommonResult<User> getUser(@PathVariable Integer id) {
        return CommonResult.success(userService.findUser(id));
    }

    @RequiresPermissions(Permissions.USER_CREATE)
    @PostMapping("/create")
    public CommonResult<User> createUser(@RequestBody User user) {
        return CommonResult.success(userService.createUser(user));
    }

    @RequiresPermissions(Permissions.USER_EDIT)
    @PostMapping("/update/{id}")
    public CommonResult<User> updateUser(@PathVariable Integer id, @RequestBody User user) {
        user = userService.updateUser(id, user);
        if (user == null) {
            return new CommonResult<User>(
                    CommonResultCode.CONCURRENT_WRITE.getCode(), CommonResultCode.CONCURRENT_WRITE.getMessage(), null);
        }

        return CommonResult.success(user);
    }

    @RequiresPermissions(Permissions.USER_DELETE)
    @PostMapping("/delete/{id}")
    public CommonResult deleteUser(@PathVariable Integer id) {
        boolean result = userService.deleteUser(id);
        if (!result) {
            return CommonResult.failed("Delete user failed", null);
        }

        return CommonResult.success();
    }

    @RequiresPermissions(Permissions.USER_ASSIGN_ROLE)
    @PostMapping("/assign-role/{userId}")
    public CommonResult assignRole(@PathVariable Integer userId, @RequestBody List<Integer> roleIds) {
        if (!roleIds.isEmpty()) {
            userService.assignRoles(userId, roleIds);
        }
        return CommonResult.success();
    }

    @RequiresPermissions(Permissions.USER_ASSIGN_ENTITY)
    @PostMapping("/assign-entity/{userId}")
    public CommonResult assignEntity(@PathVariable Integer userId, @RequestBody List<Integer> entityIds) {
        if (!entityIds.isEmpty()) {
            userService.assignEntity(userId, entityIds);
        }
        return CommonResult.success();
    }
}

